package com.mzkj.wanjulaile.mobile.api.modules.shoppingCart.service;


import com.mzkj.common.utils.Collections3;
import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.common.service.ServiceException;
import com.mzkj.wanjulaile.mobile.api.modules.appointment.entity.AppointmentCollection;
import com.mzkj.wanjulaile.mobile.api.modules.appointment.mapper.AppointmentCollectionMapper;
import com.mzkj.wanjulaile.mobile.api.modules.shoppingCart.entity.ShoppingCart;
import com.mzkj.wanjulaile.mobile.api.modules.shoppingCart.mapper.ShoppingCartMapper;
import com.mzkj.wanjulaile.mobile.api.modules.stock.entity.Stock;
import com.mzkj.wanjulaile.mobile.api.modules.stock.mapper.StockMapper;
import com.mzkj.wanjulaile.mobile.api.modules.toy.entity.Image;
import com.mzkj.wanjulaile.mobile.api.modules.toy.entity.Toy;
import com.mzkj.wanjulaile.mobile.api.modules.toy.mapper.ImageMapper;
import com.mzkj.wanjulaile.mobile.api.modules.toy.mapper.ToyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 购物车Service
 * @author ianly
 * @version 2016-10-17
 */
@Service
@Transactional(readOnly = true)
public class ShoppingCartServiceImpl  implements ShoppingCartService{
	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	@Autowired
	private ImageMapper imageMapper;

	@Autowired
	private ToyMapper toyMapper;
	@Autowired
	private StockMapper stockMapper;
	@Autowired
	private AppointmentCollectionMapper appointmentCollectionMapper;

	public ShoppingCart get(String id) {
		return shoppingCartMapper.get(id);
	}

	@Override
	public ShoppingCart get(ShoppingCart entity) {
		return shoppingCartMapper.get(entity);
	}
	
	public List<ShoppingCart> findList(ShoppingCart shoppingCart) {
		List<ShoppingCart> cartList = shoppingCartMapper.findList(shoppingCart);
		if(!Collections3.isEmpty(cartList)){
			for (ShoppingCart cart : cartList){
				setToyDetail(cart);
			}
		}
		return cartList;
	}

	public  void setToyDetail(ShoppingCart cart) {
		Stock temp = new Stock();
		temp.setRealCount(0);
		Toy toy = toyMapper.get(cart.getToy().getId());
		Stock stock = stockMapper.getByToyId(toy.getId());
		Image image = new Image();
		image.setToy(toy);
		toy.setImages(imageMapper.getListByToyId(image));
		toy.setStock(stock == null ?temp:stock);
		cart.setToy(toy);
	}


	
	@Transactional(readOnly = false)
	public void save(ShoppingCart shoppingCart) {
		if (StringUtils.isBlank(shoppingCart.getId())){
			shoppingCart.setIsNewRecord(true);
			shoppingCart.preInsert();
			shoppingCartMapper.insert(shoppingCart);
		}else{
			shoppingCart.preUpdate();
			shoppingCartMapper.update(shoppingCart);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ShoppingCart shoppingCart) {
		shoppingCartMapper.delete(shoppingCart);
	}



	/**
	 * 批量删除购物车 【用于购物车结算生产订单后，删除选中的购物车】
	 * @Title: batchDelete
	 * @param toyIds
	 * @return: boolean
	 */
	@Transactional(readOnly = false)
	public boolean batchDelete(List<String> toyIds, String accountId) {
		try {
			if(!Collections3.isEmpty(toyIds)){
				shoppingCartMapper.batchDelete(toyIds,accountId);
			}
		} catch (Exception e) {
			throw new ServiceException("批量删除购物车异常", e);
		}
		return true;
	}

	@Transactional(readOnly = false)
	public boolean batchDeleteByIds(List<String> ids) {
		try {
			shoppingCartMapper.batchDeleteByIds(ids);
		} catch (Exception e) {
			throw new ServiceException("批量删除购物车异常", e);
		}
		return true;
	}

	@Transactional(readOnly = false)
	public boolean deleteByAccountId(Long accountId) {
		try {
			shoppingCartMapper.deleteByAccountId(accountId);
		} catch (Exception e) {
			throw new ServiceException("删除购物车异常", e);
		}
		return true;
	}

	/**
	 * 购物车ids查询
	 * @param ids
	 * @return
     */
	public List<ShoppingCart> findListByIds(List<String> ids){
		List<ShoppingCart> cartList = shoppingCartMapper.findListByIds(ids);
		if(!Collections3.isEmpty(cartList)){
			for (ShoppingCart cart : cartList){
				setToyDetail(cart);
			}
		}
		return cartList;
	}

	/**
	 *  购物车玩具数量加减用
	 * @param shoppingCart
	 * @return
     */
	@Transactional(readOnly = false)
	public ShoppingCart update(ShoppingCart shoppingCart) {
		try {
			shoppingCartMapper.update(shoppingCart);
		} catch (Exception e) {
			throw new ServiceException("更新购物车异常", e);
		}
		return shoppingCartMapper.get(shoppingCart.getId());
	}

	/**
	 * 获取购物车列表
	 * @param shoppingCart
	 * @return
     */
	public List<ShoppingCart> findListByAccountId(ShoppingCart shoppingCart){
		return shoppingCartMapper.findListByAccountId(shoppingCart);
	}

	/**
	 *  判断同一商品是否重复加入购物车
	 * @param shoppingCart
	 * @return
     */
	public List<ShoppingCart> getByAcIdToyId(ShoppingCart shoppingCart){
		return shoppingCartMapper.getByAcIdToyId(shoppingCart);
	}


	public List<ShoppingCart> findList2ByAccountId(String accountId) {
		return shoppingCartMapper.findList2ByAccountId(accountId);
	}

	@Override
	@Transactional
	public void delBoth(ShoppingCart shoppingCart, AppointmentCollection appointmentCollection) {
		shoppingCartMapper.delete(shoppingCart);
		appointmentCollectionMapper.deleteByAccountIdAndToy(appointmentCollection);
	}
}